{
 "metadata": {
  "name": "",
  "signature": "sha256:296d6fed9d8a5715a898fa9c1d11ddbad2196e829890bfc3f81f7672df3e47db"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import spynner, os, sys\n",
      "import time\n",
      "import os\n",
      "from cStringIO import StringIO\n",
      "from PyQt4.QtWebKit import QWebSettings\n",
      "from IPython.html.widgets import ImageWidget, HTMLWidget, IntProgressWidget\n",
      "from IPython.display import display, Image\n",
      "from PyQt4.QtCore import QBuffer, QIODevice\n",
      "from PyQt4.QtGui import QImage\n",
      "import cv2\n",
      "import numpy as np\n",
      "from PIL import Image as PIL_Image\n",
      "from IPython.display import display, Image\n",
      "from io import BytesIO\n",
      "def img_to_png(ima, cvt=None):\n",
      "    if cvt:\n",
      "        ima = cv2.cvtColor(ima, cvt)\n",
      "    im = PIL_Image.fromarray(ima)\n",
      "    bio = BytesIO()\n",
      "    im.save(bio, format='png')\n",
      "    return bio.getvalue()\n",
      "def display_img_array(ima, cvt=None, **kwargs):\n",
      "    display(Image(img_to_png(ima, cvt=cvt) , format='png', **kwargs))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "debug_stream = StringIO()\n",
      "browser = spynner.Browser(debug_level=spynner.ERROR, debug_stream=sys.stderr)\n",
      "browser.create_webview()\n",
      "browser.show()\n",
      "settings = browser.webview.settings()\n",
      "# settings.setAttribute(QWebSettings.AutoLoadImages, False)   # uncomment this if you only need text\n",
      "browser.webview.setFixedHeight(720)\n",
      "browser.webview.setFixedWidth(1280)\n",
      "browser.webview.resize(1280, 720)\n",
      "settings.setFontFamily(2, u'\u601d\u6e90\u9ed1\u4f53')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "url = \"file:///home/tjw/src/hualienpy/pyconcn_zhs2.html\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "browser.load(url)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 66,
       "text": [
        "True"
       ]
      }
     ],
     "prompt_number": 66
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "for i in range(100):\n",
      "    browser.webview.repaint()\n",
      "    browser.sendKeys('body',[ord(' ')])\n",
      "    time.sleep(1)\n",
      "    browser.webview.repaint()\n",
      "    browser.webview.repaint()\n",
      "    fn = \"s\"+browser.url[browser.url.find('#/')+2:].replace('/', '_')+\".png\"\n",
      "    print fn\n",
      "    sys.stdout.flush()\n",
      "    buffer = QBuffer()\n",
      "    buffer.open(QIODevice.ReadWrite)\n",
      "    qimg = browser.snapshot().save(buffer, \"PNG\")\n",
      "    strio = StringIO()\n",
      "    strio.write(buffer.data())\n",
      "    buffer.close()\n",
      "    strio.seek(0)\n",
      "    pimg = PIL_Image.open(strio).convert('RGBA')\n",
      "    new_data = []\n",
      "    for c in pimg.getdata():\n",
      "        if max(c[0],c[1],c[2])-min(c[0],c[1],c[2])<10 and c[0] > 128:\n",
      "            new_data.append((0,0,0,int(385-c[0]*3/2)  ))\n",
      "        else:\n",
      "            new_data.append(c)\n",
      "    pimg.putdata(new_data)\n",
      "    pimg.save(fn, 'PNG')\n",
      "    if 'The' in fn:\n",
      "        break\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s1.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s1_1.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s1_2.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s1_3.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s1_4.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s1_5.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s2.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s2_1.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s2_2.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s2_3.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s2_4.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s2_5.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s3.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s3_1.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s3_2.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s3_3.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s3_4.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s3_5.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s3_6.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s3_7.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s3_8.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s4.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s4_1.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s4_2.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s4_3.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s4_4.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s4_5.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s4_6.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s4_7.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s4_8.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s4_9.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s4_10.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "s4_11.png\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "sThe.png\n"
       ]
      }
     ],
     "prompt_number": 67
    }
   ],
   "metadata": {}
  }
 ]
}